<template>
  <div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '订单详情',
                tooltip: '订单详情',
                onClick: handleDetail.bind(null, record),
              },
            ]"
        /></template>
      </template>
      <template #toolbar>
        <a-button type="primary">导出订单</a-button>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getBasicColumns } from './tableData';
  import Dictionary from '@/utils/dictionary';
  import { OrderList } from '@/api/membership';
  import { IOrderListResult } from '@/api/membership/type';
  import { useRouter } from 'vue-router';

  const dicts = Dictionary.getLocalMulti(['orderState', 'orderType', 'payType', 'payMode']);
  const router = useRouter();

  const [registerTable] = useTable({
    title: '开启搜索区域',
    api: OrderList,
    columns: getBasicColumns(),
    formConfig: {
      labelWidth: 100,
      schemas: [
        {
          field: `orderCode`,
          label: `订单编号`,
          component: 'Input',
          colProps: {
            span: 4,
          },
        },
        {
          field: `orderType`,
          label: `订单类型`,
          component: 'Select',
          componentProps: {
            fieldNames: { label: 'dictLabel', value: 'dictValue' },
            options: dicts.orderType,
          },
          colProps: {
            span: 4,
          },
        },
        {
          field: `type`,
          label: `缴费类型`,
          component: 'Select',
          componentProps: {
            fieldNames: { label: 'dictLabel', value: 'dictValue' },
            options: dicts.payType,
          },
          colProps: {
            span: 4,
          },
        },
        {
          field: `orderState`,
          label: `订单状态`,
          component: 'Select',
          componentProps: {
            fieldNames: { label: 'dictLabel', value: 'dictValue' },
            options: dicts.orderState,
          },
          colProps: {
            span: 4,
          },
        },
        {
          field: `payTime`,
          label: `付款时间`,
          component: 'RangePicker',
          colProps: {
            span: 6,
          },
        },
      ],
    },
    beforeFetch: (parmas) => {
      let payTime = parmas.payTime || [];
      return {
        ...parmas,
        startTime: payTime[0],
        endTime: payTime[1],
        payTime: undefined,
      };
    },
    useSearchForm: true,
    showTableSetting: true,
    tableSetting: { fullScreen: true },
    showIndexColumn: true,
    clickToRowSelect: false,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
    },
    afterFetch: (data: IOrderListResult[]) => {
      return data.map((item) => {
        return {
          ...item,
          orderStateText: dicts.orderState.find((v) => v.dictValue == item.orderState)?.dictLabel,
          orderType: dicts.orderType.find((v) => v.dictValue == item.orderType)?.dictLabel,
          type: dicts.payType.find((v) => v.dictValue == item.type)?.dictLabel,
          payMode: dicts.payMode.find((v) => v.dictValue == item.payMode)?.dictLabel,
        };
      });
    },
  });

  function handleDetail(row: IOrderListResult) {
    router.push({
      path: '/membership/orderDetail',
      query: { id: row.id },
    });
  }
</script>
